﻿Imports System.Data
Imports System.Data.Odbc



Public Class frmPersona

    Dim res As DialogResult
    Dim id As Integer
    Private Sub btnIngresar_Click(sender As Object, e As EventArgs) Handles btnIngresar.Click
        Try


            If txtNombre.Text = "" Or txtApPaterno.Text = "" Or txtApeMaterno.Text = "" Or cbTipo.SelectedIndex = -1 Then
                MessageBox.Show(msjCampoBlanco, msjTituloModal, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                txtRun.Focus()
            ElseIf txtRun.TextLength > 0 Then
                res = MessageBox.Show(msjIngreso, msjTituloModal, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                If validarRut(txtRun.Text) And res = Windows.Forms.DialogResult.Yes Then

                    ingresoPersona(capturaRut, txtNombre.Text, txtApeMaterno.Text, txtApPaterno.Text, cbTipo.SelectedValue, txtFono.Text, txtCelular.Text, txtDomicilio.Text, txtEmail.Text)

                    txtRun.Clear()
                    txtNombre.Clear()
                    txtApeMaterno.Clear()
                    txtApPaterno.Clear()
                    cbTipo.SelectedIndex = -1
                    txtFono.Clear()
                    txtCelular.Clear()
                    txtDomicilio.Clear()
                    txtEmail.Clear()

                    txtRun.Focus()


                Else
                    txtRun.Focus()
                    MessageBox.Show("El rut ingresado NO es válido.", msjTituloModal, MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

             
            ElseIf txtRun.TextLength = 0 Then

                res = MessageBox.Show(msjIngreso, msjTituloModal, MessageBoxButtons.YesNo, MessageBoxIcon.Question)


                If res = Windows.Forms.DialogResult.Yes Then

                    ingresoPersona(capturaRut, txtNombre.Text, txtApeMaterno.Text, txtApPaterno.Text, cbTipo.SelectedValue, txtFono.Text, txtCelular.Text, txtDomicilio.Text, txtEmail.Text)

                    txtRun.Clear()
                    txtNombre.Clear()
                    txtApeMaterno.Clear()
                    txtApPaterno.Clear()
                    cbTipo.SelectedIndex = -1
                    txtFono.Clear()
                    txtCelular.Clear()
                    txtDomicilio.Clear()
                    txtEmail.Clear()

                    txtRun.Focus()

                Else
                    txtRun.Focus()
                 
                End If


            End If


        Catch ex As Exception
            mensajeError(ex)
        End Try
    End Sub

    Private Sub frmPersona_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            listaPersonas()
            datosCBTipo()
   
        Catch ex As Exception
            mensajeError(ex)
        End Try

    End Sub

 

 


    Private Sub btnEliminar_Click(sender As Object, e As EventArgs) Handles btnEliminar.Click

        res = MessageBox.Show("¿Esta seguro(a) de eliminar este campo?", "Coprevi", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

        Try
            With DGV_Personas

                If Not .CurrentRow.IsNewRow And res = Windows.Forms.DialogResult.Yes Then

                    id = .Item(0, .CurrentRow.Index).Value
                    eliminarPersona(id)
                Else
                    MessageBox.Show("No se pudo eliminar los datos, favor seleccione el código para eliminar.", "Coprevi", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If


            End With

          
        Catch ex As Exception
            mensajeError(ex)
        End Try

    End Sub

  
    Private Sub btnEditar_Click(sender As Object, e As EventArgs) Handles btnEditar.Click
        Dim id As Integer
        Dim rut As String = ""
        Dim nom As String = ""
        Dim app As String = ""
        Dim apm As String = ""

        Dim fono As String = ""
        Dim celular As String = ""
        Dim domicilio As String = ""
        Dim mail As String = ""

        Try

            With DGV_Personas

                res = MessageBox.Show(msjActualizar, msjTituloModal, MessageBoxButtons.YesNo, MessageBoxIcon.Question)

                If .CurrentRow Is Nothing And res = Windows.Forms.DialogResult.Yes Then

                    MessageBox.Show(msjDatoAActualizar, msjTituloModal, MessageBoxButtons.OK, MessageBoxIcon.Information)

                ElseIf .CurrentRow IsNot Nothing And res = Windows.Forms.DialogResult.Yes Then

                    id = .Item(0, .CurrentCell.RowIndex).Value
                    rut = .Item(1, .CurrentCell.RowIndex).Value.ToString
                    nom = .Item(2, .CurrentCell.RowIndex).Value.ToString
                    app = .Item(3, .CurrentCell.RowIndex).Value.ToString
                    apm = .Item(4, .CurrentCell.RowIndex).Value.ToString
            
                    fono = .Item(6, .CurrentCell.RowIndex).Value.ToString
                    celular = .Item(7, .CurrentCell.RowIndex).Value.ToString
                    domicilio = .Item(8, .CurrentCell.RowIndex).Value.ToString
                    mail = .Item(9, .CurrentCell.RowIndex).Value.ToString


                    actualizarPersona(id, rut, nom, app, apm, fono, celular, domicilio, mail)
                End If

            End With



        Catch ex As Exception
            mensajeError(ex)

        End Try
    End Sub

    Private Sub btnBuscar_Click(sender As Object, e As EventArgs) Handles btnBuscar.Click

        Try
            If txtBusRun.TextLength = 0 Then
                listaPersonas()
            Else
                listaPersonasBus(txtBusRun.Text)
                txtBusRun.Clear()
            End If
     
        Catch ex As Exception
            mensajeError(ex)

        End Try
       

    End Sub


  


    Private Sub txtRun_Validated(sender As Object, e As EventArgs) Handles txtRun.Validated
        If Not validarRut(txtRun.Text) And txtRun.TextLength > 0 Then
            txtRun.Focus()
            MessageBox.Show("Favor,ingrese un RUN válido", "RUN incorrecto", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

    End Sub



End Class